import sys

import pygame


class Maze:
    def __init__(self, x, y, size, arr, color=(255, 255, 255), color_bg=(0, 0, 0), width=1, filled=False):
        self.x = x  # maze 左上角x坐标
        self.y = y  # maze 左上角y坐标
        self.size = size  # int 每个正方形宫格的边长
        self.arr = arr
        self.color = color
        self.color_bg = color_bg
        self.width = width
        self.filled = filled

    def judge_neighbor(self, i, j):
        '''
        根据给定的下标, 判断二维数组上下左右的值,并依次返回
        要求i,j要为int
        :param i: 行
        :param j: 列
        :return: 元祖(上,下,左,右)的值
        '''
        i = int(i)
        j = int(j)
        len_row = len(self.arr)
        len_col = len(self.arr[i])
        # print(len_row, len_col)
        if i == 0:  # 第一行
            up = None
        else:
            up = self.arr[i - 1][j]
        if j == 0:
            left = None
        else:
            left = self.arr[i][j - 1]
        try:
            down = self.arr[i + 1][j]
        except:
            down = None
        try:
            right = self.arr[i][j + 1]
        except:
            right = None
        return up, down, left, right

    def draw(self, surface):
        for i, row in enumerate(self.arr):
            for j, col in enumerate(row):
                # print(i, j, col)
                points = self.get_point(i, j)
                if not self.judge_neighbor(i, j)[0]:
                    # draw up
                    pygame.draw.line(surface, self.color, (points[0][0], points[0][1]), (points[1][0], points[1][1]))
                if not self.judge_neighbor(i, j)[1]:
                    # draw down
                    pygame.draw.line(surface, self.color, (points[2][0], points[2][1]), (points[3][0], points[3][1]))
                if not self.judge_neighbor(i, j)[2]:
                    # draw left
                    pygame.draw.line(surface, self.color, (points[0][0], points[0][1]), (points[3][0], points[3][1]))
                if not self.judge_neighbor(i, j)[3]:
                    # draw right
                    pygame.draw.line(surface, self.color, (points[1][0], points[1][1]), (points[2][0], points[2][1]))

    def get_point(self, i, j):
        # 根据给定的索引, 计算该宫格四个顶点以及中心点坐标
        top_left = self.x + self.size * j, self.y + self.size * i
        # top_left =  self.size * j, self.size * i
        top_right = top_left[0] + self.size, top_left[1]
        bottom_left = top_left[0], top_left[1] + self.size
        bottom_right = top_left[0] + self.size, top_left[1] + self.size
        center = top_left[0] + self.size / 2, top_left[1] + self.size / 2
        return top_left, top_right, bottom_right, bottom_left, center

    def get_indexes(self, x, y):
        # 根据给定坐标值(绝对值, 也就是整个窗口的坐标值), 计算当前二维数组索引位置
        j = (x - self.x) // self.size
        i = (y - self.y) // self.size
        if (x - self.x) % self.size == 0:
            # if 中心点落在每个格子的边线上 无法判断属于哪个格子
            return i, None
        if  (y - self.y) % self.size == 0:
            return None, j
        return i, j


if __name__ == '__main__':
    arr = [[1, 0, 0, 0, 1, 1]
        , [1, 1, 1, 1, 1, 0]
        , [0, 1, 0, 0, 0, 0]
        , [0, 1, 0, 0, 0, 0]
        , [0, 1, 0, 0, 0, 0]
        , [0, 1, 1, 0, 0, 0]]
    maze = Maze(0, 0, 10, arr)
    winx = winy = 300
    screen = pygame.display.set_mode((winx, winy))
    print(maze.get_point(1, 0))
    pygame.draw.circle(screen,(255,255,255),(maze.get_point(0, 1)[4]),2)
    print(maze.get_indexes(5, 11))
    while True:
        # screen.fill(maze.color_bg)
        maze.draw(screen)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()

        pygame.display.update()
